/***************************************************************************
 *   Copyright (C) 2008 by Eduardo Gurgel Pinho                            *
 *   edgurgel@gmail.com                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
***************************************************************************/

#ifndef __SPHERE_H__
#define __SPHERE_H__

#include "object.h"
#include "math/vector.h"
#include "math/ray.h"
#include "material/object_material.h"

#include <boost/tuple/tuple.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/version.hpp>

using namespace boost::tuples;

namespace Sirat {

namespace Object {

/**
 * Sphere class.
 * Defined by its center and radius.
 */
	
class Sphere : public Object {
public:
	/**
	* Simple constructor.
	* @param _material Object's material.
	* @param _center Sphere's center.
	* @param _radius Sphere's radius.
	 */
	Sphere(Material::Object_Material* _object_material,const Math::Vector& _center,const double _radius);
	
	/**
	 * Default constructor.
	 * Boost Serialization needs a default constructor
	 * @see Sphere(const Sphere&)
	 */
	Sphere();
	
	/**
	 * Copy constructor.
	 * @param _sphere
	 */
	Sphere(const Sphere& _sphere);
	
	/**
	 * Destructor
	 */
	~Sphere();

	/**
	 * @return Current center.
	 */
	Math::Vector get_center() const;
	
	/**
	 * @return Current radius.
	 */
	double get_radius() const;
	
	/**
	 * Normal vector at any given point.
	 * @param _point
	 * @return Sphere's normal vector.
	 */
	virtual Math::Vector get_normal(const Math::Vector& _point) const;
	
	/**
	 * Change sphere's center.
	 * @param _center
	 */
	void set_center(const Math::Vector& _center);
	
	/**
	 * Change sphere's radius.
	 * @param _radius
	 */
	void set_radius(const double _radius);

	/**
	 * Main function.
	 * @param _ray 
	 * @return Intersection point and the distance from ray's origin.
	 */
	virtual tuple<double,Math::Vector,Object*> intersection(const Math::Ray&);
	
	virtual tuple<double,double> get_u_v(const Math::Vector& _point) const;

	Sphere& operator=(const Sphere& _sphere);

	Object* clone() { return new Sphere(*this); }
	
private:
	friend class boost::serialization::access;
	template<class Archive> void serialize(Archive&, const unsigned int);

	Math::Vector center;
	double radius;
};



template<class Archive> void Sphere::serialize(Archive& ar, const unsigned int /* file_version */){
	ar	& BOOST_SERIALIZATION_BASE_OBJECT_NVP(Object)
		& BOOST_SERIALIZATION_NVP(center)
		& BOOST_SERIALIZATION_NVP(radius);              
}

}

}

#endif
